package cn.restcloud.example.controller;

import java.util.HashMap;
import java.util.Map;
import cn.restcloud.example.model.UserModel;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestAuthType;
import cn.restcloud.framework.core.annotation.rest.BodyDataType;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.annotation.rest.RestRequestBody;
import cn.restcloud.framework.core.context.AppContext;
import cn.restcloud.framework.core.util.DateTimeUtil;
import cn.restcloud.framework.core.util.PrintUtil;
import cn.restcloud.framework.validate.ValidType;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;

/**
 * HelloWorld 服务示例
 * @author cn.restcloud
 *
 */
@BeanConfig(appId = "example",beanId = "HelloWorld",beanName = "HelloWorld我是一个Rest服务类",beanType = BeanType.CONTROLLER)
@RestConfig(url = "/rest/example/hello")
public class HelloWorld {
	
	/**
	 * 浏览器中输入 http://localhost:8080/restcloud/rest/example/hello/admin 即可访问本服务
	 * @param userId
	 * @return json string
	 * @throws Exception
	 */
	@RestConfig(configName = "HelloWorld你好",configId="HelloWord",method = MethodType.GET,url = "/{userid}")
	@Params({
		@Param(id = "userid", name = "用户名",required=true)
	})
	public String helloWorld(String userId) throws Exception{
		String msg=userId+"您好!!!!";
		String jsonStr="{\"msg\":\""+msg+"\"}";
		return jsonStr;
	}
	
	/**
	 * 浏览器中输入 http://localhost:8080/restcloud/rest/example/hello/testParams?num=1  即可访问本服务
	 * @return
	 * @throws Exception
	 */
	@RestAuthType("0") //权限认证类型0表示匿名访问,1表示需要认证,2表示需要审批
	//@RestPermissions("Hello_Test") //在需要认证时可以绑定的权限id
	@RestConfig(configName = "输入参数验证",configId="HelloWordParams",method = MethodType.GET,url = "/testParams")
	@Params({
		@Param(id = "num", name = "请传入一个数字",required=true,validateBeanId=ValidType.ISNUMERIC,tip="必须输入一个数字"),
	})
	public DocAndView paramsTest(int num) throws Exception{
//		int num=Integer.valueOf(RequestUtil.getString("num")); //也可以使用RequestUtil取传入参数
		num++;
		String jsonStr="{\"num\":\""+num+"\"}";
		return ViewUtil.getJsonView(jsonStr);
	}
	
	@RestRequestBody(type=BodyDataType.JSON,cls=UserModel.class)
	@RestConfig(configName = "传入JSON字符串转换为Java Bean对像",configId="HelloWordBeanTest",method = MethodType.POST,url = "/javabean")
	public DocAndView getJson2JavaBean(UserModel userObj) throws Exception{
		String returnStr="userid="+userObj.getUserId()+",username="+userObj.getUserName();
//		PrintUtil.o(returnStr);
		return ViewUtil.getJsonView(userObj); //直接把java bean转为json再输出
	}
	
	//通过使用props属性可以指 定API中的扩展属性定义,web界面中的所有配置都可以使用props来指定，多个用逗号分隔
	@RestRequestBody
	@RestConfig(configName = "RequestBody请求测试",method = MethodType.POST,url = "/requestBodyTest",props="viewId:hello.ftl")
	public Map<String,String> requetBodyTest(String params) throws Exception{
		PrintUtil.o(params);
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("requestBody", params);
		map.put("nowtime", DateTimeUtil.getNow());
		map.put("userId", AppContext.getUserId());
		map.put("hello","hello1223");
		return map;
	}
	
}
